package com.zl.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.system.comm.util.DateUtils;
import com.system.comm.util.Snowflake;
import com.system.common.mapper.BisBagProjectMapper;
import com.system.common.mapper.BisBagRecordMapper;
import com.system.common.mapper.BisBeautyRecordMapper;
import com.system.common.mapper.BisBusinessOrderMapper;
import com.system.common.mapper.BisCustomUserMapper;
import com.system.common.mapper.BisNurseBagMapper;
import com.system.common.mapper.BisOrderPayMapper;
import com.system.common.mapper.BisProjectRecordMapper;
import com.system.common.pojo.BisBagProject;
import com.system.common.pojo.BisBagProjectExample;
import com.system.common.pojo.BisBagRecord;
import com.system.common.pojo.BisBagRecordExample;
import com.system.common.pojo.BisBeautyRecord;
import com.system.common.pojo.BisBusinessOrder;
import com.system.common.pojo.BisCustomUser;
import com.system.common.pojo.BisNurseBag;
import com.system.common.pojo.BisNurseBagExample;
import com.system.common.pojo.BisNurseBagExample.Criteria;
import com.system.common.pojo.BisOrderPay;
import com.system.common.pojo.BisProjectRecord;

@Service
public class ZlBisNurseBagServiceImpl implements ZlBisNurseBagService{
	@Autowired
	private BisNurseBagMapper bisNurseBagMapper;
	@Autowired
	private BisBagProjectMapper BisBagProjectMapper;
    @Autowired
    private BisBagRecordMapper bisBagRecordMapper;
    @Autowired
    private BisOrderPayMapper bisOrderPayMapper;
    @Autowired
    private BisBusinessOrderMapper bisBusinessOrderMapper;
    @Autowired
    private BisCustomUserMapper bisCustomUserMapper;
    @Autowired
    private BisBeautyRecordMapper bisBeautyRecordMapper;
    @Autowired
    private BisProjectRecordMapper bisProjectRecordMapper;
	@Override
	public List<BisNurseBag> selectByExample(Map<String, Object> map) {
		BisNurseBagExample example=new BisNurseBagExample();
		Criteria criteria=example.createCriteria();
		criteria.andStatusEqualTo("1");
		Date date=new Date();
		criteria.andSoldoutTimeGreaterThan(date);
		
		return this.bisNurseBagMapper.selectByExample(example);
	}

	@Override
	public List<BisNurseBag> bisNurseBagsService(List<BisNurseBag> bisNurseBags) {
		List<BisNurseBag> bisNurseBagList = new ArrayList<>() ;
		Date datee=new Date();
		
		for (BisNurseBag bisNurseBag : bisNurseBags) {
			Date date=bisNurseBag.getSoldoutTime();
			
			long time= date.getTime()-datee.getTime();
			
			long mm=time/(60*1000)%60;
			long hh=time/(60*1000*60)%24;
			long dd=time/(60*1000*60*24);
			String orderDay=dd+"天"+hh+"小时"+mm+"分";
			bisNurseBag.setBagIntroduce(orderDay);
			
			bisNurseBagList.add(bisNurseBag);
		}
		return bisNurseBagList;
	}

	@Override
	public BisNurseBag queryById(String bagId) {
		
		return bisNurseBagMapper.selectByPrimaryKey(bagId);
	}

	@Override
	public List<BisBagProject> queryBagProjectByBagId(String bagId) {
		BisBagProjectExample example=new BisBagProjectExample();
		com.system.common.pojo.BisBagProjectExample.Criteria criteria=example.createCriteria();
		criteria.andBagIdEqualTo(bagId);
		return BisBagProjectMapper.selectByExample(example);
	}

	@Override
	public List<BisBagRecord> queryUserBag(String userId) {
		BisBagRecordExample example=new BisBagRecordExample();
		com.system.common.pojo.BisBagRecordExample.Criteria criteria=example.createCriteria();
		criteria.andCustomUserIdEqualTo(userId);
		List<BisBagRecord> bisList=this.bisBagRecordMapper.selectByExample(example);
		for (BisBagRecord bisBagRecord : bisList) {
			BisNurseBag bisNurseBag=bisNurseBagMapper.selectByPrimaryKey(bisBagRecord.getBagId());
			int dayNum=bisNurseBag.getOverdueTime();
			String bagName=bisNurseBag.getBagName();
			Date date=DateUtils.getAddDayForDate(bisBagRecord.getCreateTime(), dayNum);
			bisBagRecord.setOverdueDate(DateUtils.getYMDHMSFormat(date));
			bisBagRecord.setRecordId(bagName);
			
		}
		// TODO Auto-generated method stub
		return bisList;
	}

	@Override
	public Map<String, Object> checkRobBag(String bagId, BisCustomUser bisCustomUser) {
		System.out.println(bagId);
		 bagId=bagId.substring(1,bagId.length()-1);
		 System.out.println(bagId);
		BisNurseBag bisNurseBag=bisNurseBagMapper.selectByPrimaryKey(bagId);
		BisBagRecordExample example=new BisBagRecordExample();
		com.system.common.pojo.BisBagRecordExample.Criteria criteria=example.createCriteria();
		criteria.andBagIdEqualTo(bagId);
		criteria.andCustomUserIdEqualTo(bisCustomUser.getCustomUserId());
		criteria.andRecordTypeEqualTo("1");
		List<BisBagRecord> bagList=new ArrayList<>();
		bagList=this.bisBagRecordMapper.selectByExample(example);
		Map<String, Object> map=new HashMap<>();
		System.out.println(bisNurseBag.getBuyCount());
		if (bagList.size()>=bisNurseBag.getBuyCount()) {
			map.put("state", "2");
			map.put("msg", "您已购买了最大数量");
		}else {
			map.put("state", "1");
		}
		return map;
	}
    
	
	
	@Transactional
	public Map<String, Object> checkBeauty(HttpServletRequest request, BisCustomUser bisCustomUser) {
		String orderMoney=request.getParameter("orderMoney");
		String bagId=request.getParameter("bagId");
		String buyNum=request.getParameter("buyNum");
		int BeautyNum=bisCustomUser.getBeautyNum();
		Map<String, Object> map=new HashMap<>();
		
		if (Double.parseDouble(orderMoney)>BeautyNum) {
			map.put("msg", "美颜币不足青葱之");
			return  map;
		}
		
		bagId=bagId.substring(1, bagId.length()-1);
		System.out.println("<>"+bagId);
		BisNurseBag bisNurseBag=bisNurseBagMapper.selectByPrimaryKey(bagId);
		
		if (bisNurseBag.getRemainNum()<Integer.parseInt(buyNum)) {
			map.put("msg", "礼包不足！");
			return map;
		}
        BisOrderPay orderPayPO = new BisOrderPay();
		
        Snowflake snowflake=new Snowflake(0, 0);
		String payId=snowflake.nextId()+"";
		String orderId=snowflake.nextId()+"";
		orderPayPO.setPayId(payId);
		orderPayPO.setOrderId(orderId);
		orderPayPO.setBuyAccount(bisCustomUser.getOpenid());
		orderPayPO.setPayCode("b"+payId);
		orderPayPO.setPayWay("1");
		orderPayPO.setPayStatus("1");
		orderPayPO.setCreateTime(new Date());
		orderPayPO.setPayMoney(Double.parseDouble(orderMoney));
		orderPayPO.setPayType("1");
		orderPayPO.setPayBack("2");
		orderPayPO.setBuyAccount(bisCustomUser.getOpenid());
		bisOrderPayMapper.insert(orderPayPO);
		
		String order_content = "购买"+buyNum+"份"+bisNurseBag.getBagName();
		BisBusinessOrder orderPO = new BisBusinessOrder();
		orderPO.setOrderId(orderId);
		orderPO.setProjectId(bagId);
		orderPO.setCustomUserId(bisCustomUser.getCustomUserId());
		orderPO.setOrderContent(order_content);
		orderPO.setOrderMoney(Double.parseDouble(orderMoney));
		orderPO.setCreateTime(new Date());
		orderPO.setOrderStatus("0");
		orderPO.setOrderSource("2");
		orderPO.setOrderType("3");
		orderPO.setBuyNum(Integer.parseInt(buyNum));
		int row=bisBusinessOrderMapper.insert(orderPO);
		if (row > 0) {
			map.put("state", "1");
			map.put("msg", "操作完成!");
			orderPayPO.setPayBack("1");
			orderPayPO.setPayStatus("3");
			bisOrderPayMapper.updateByPrimaryKey(orderPayPO);
			
			orderPO.setOrderStatus("4");
			bisBusinessOrderMapper.updateByPrimaryKey(orderPO);
			double beauty=bisCustomUser.getBeautyNum();
			beauty=beauty-Double.parseDouble(orderMoney);
			bisCustomUser.setBeautyNum((int)beauty);
			int num=bisCustomUserMapper.updateByPrimaryKey(bisCustomUser);
			if (num<0) {
				map.put("msg", "支付失败！");
			}
			//保存美颜币消耗记录
			BisBeautyRecord bisBeautyRecord=new BisBeautyRecord();
			String recordId=snowflake.nextId()+"";
			bisBeautyRecord.setRecordId(recordId);
			bisBeautyRecord.setOrderId(orderId);
			bisBeautyRecord.setCustomUserId(bisCustomUser.getCustomUserId());
			bisBeautyRecord.setBeautyNum((int)Double.parseDouble(orderMoney));
			bisBeautyRecord.setRecordType("2");
			bisBeautyRecord.setPayTime(new Date());
			bisBeautyRecord.setVaildStatus("1");
			bisBeautyRecordMapper.insert(bisBeautyRecord);
			
			//保存礼包
			BisBagRecord bisBagRecord=new BisBagRecord();
			String recordIdd=snowflake.nextId()+"";
			bisBagRecord.setRecordId(recordIdd);
			bisBagRecord.setBagId(bagId);
			bisBagRecord.setCustomUserId(bisCustomUser.getCustomUserId());
			bisBagRecord.setBuyNum(Integer.parseInt(buyNum));
			bisBagRecord.setCreateTime(new Date());
			bisBagRecord.setOrderId(orderId);
			bisBagRecord.setRecordType("1");
			bisBagRecord.setStatus("1");
			bisBagRecord.setReceiveStatus("1");
			bisBagRecord.setShareNum(2);
			bisBagRecordMapper.insert(bisBagRecord);
			int remainNum=bisNurseBag.getRemainNum();
			bisNurseBag.setRemainNum(remainNum-Integer.parseInt(buyNum));
			bisNurseBagMapper.updateByPrimaryKey(bisNurseBag);
			
			
			BisProjectRecord bisProjectRecord=new BisProjectRecord();
			
			BisBagProjectExample example=new BisBagProjectExample();
			com.system.common.pojo.BisBagProjectExample.Criteria criteria=example.createCriteria();
			criteria.andBagIdEqualTo(bagId);
			List<BisBagProject> bpList=this.BisBagProjectMapper.selectByExample(example);
			for (BisBagProject bisBagProject : bpList) {
				String recordIdds=snowflake.nextId()+"";
				bisProjectRecord.setRecordId(recordIdds);
				bisProjectRecord.setProjectId(bisBagProject.getProjectId());
				bisProjectRecord.setBagRecordId(bagId);
				bisProjectRecord.setProjectStatus("1");
				bisProjectRecordMapper.insert(bisProjectRecord);
			}
			
			
		} else {
			map.put("msg", "操作完成，购买礼包失败，礼包订单无法保存成功。");
		
		}
		return map;
	}

}
